package com.yoomiito.app.ui.yijiayou

import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.base.WebViewActivity
import com.yoomiito.app.model.LatLng
import com.yoomiito.app.model.oil.OilStationDetailData
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.showToast
import com.yoomiito.app.ui.anyview.gasstation.GasSortTabView
import com.yoomiito.app.widget.dialog.SelectMapDialog

abstract class BaseGasStationActivity<T : BaseActPresenter<*>> : BaseActivity<T>() {

    var oilNum = GasSortTabView.DefaultOilNum

    fun oilDetail(id: String, transformLatLng: LatLng, originallatLng: LatLng, type: Int) {
        YmApi.getYmService()
                .stationDetail(id, transformLatLng.lng, transformLatLng.lat, originallatLng.lng, originallatLng.lat, type)
                .compose(bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<OilStationDetailData>() {
                    override fun onSuccess(t: OilStationDetailData?) {
                        if (t==null||(t.type==2&&t.detail.isNullOrEmpty())){//团油详情的油号可能为空
                            "油站数据有误，请稍后再试".showToast()
                        }else{
                            toDetail(t)
                        }
                    }

                    override fun onFail(error: ApiException?) {
                        dismissLoading()
                    }
                })
    }

    fun toDetail(t: OilStationDetailData?) {
        dismissLoading()
        t?.let {
            when (t.type) {
                2,4 -> {
                    t.selectOilNum = oilNum
//                    t.selectOilNum = -40
                    GasStationDetailActivity.start(this, t)
                }
                else -> {
                    WebViewActivity.start(this, t.url)
                }
            }
        }
    }

    fun showSelectMap(lat: Double, lng: Double) {
        val dialog = SelectMapDialog(this, lat, lng)
        dialog.show()
    }
}